//
//  TanHunLunJiaViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "XLPageViewController.h"
#import "HuDongFunctionView.h"
#import "LSTPopView.h"
#import "CommunitySegCell.h"

#import "CommunityBottomView.h"


#import "LunJiaMainViewController.h"
#import "TanHunMainViewController.h"

#import "PostPublishViewController.h"
#import "TopicPublishViewController.h"
#import "QAPublishViewController.h"

#import "MyTopicListViewController.h"
#import "MyQAListViewController.h"
#import "MyPostListViewController.h"
#import "MyEvaluateViewController.h"
#import "MyHDSegViewController.h"

#import "TanHunLunJiaViewController.h"

@interface TanHunLunJiaViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) UIButton *actionBtn;

@property (nonatomic, assign) BOOL isShow;

@property (nonatomic, strong) HuDongFunctionView *huDongView;
@property (nonatomic, strong) LSTPopView *popView;

@property (nonatomic, strong) CommunityBottomView *bottomView;
@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) TanHunMainViewController *tanhunVC;
@property (nonatomic, strong) LunJiaMainViewController *lunjiaVC;
@property (nonatomic, assign) CGFloat latest_y;


@end

@implementation TanHunLunJiaViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    

    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
       
        if (isLogin) {
            if (!self.pageViewController) {
                [self initPageViewController];
            }
            [self.view insertSubview:self.bottomView atIndex:[[self.view subviews] count]];

            [[TT_MessageManager manager] getInteractionCount:^{}];
            [self.icon sd_setImageWithURL:[NSURL URLWithString:[TTUserModel sharedObject].userIcon] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        }
    }];
    
}

- (void)initView {

    self.view.backgroundColor = UIColorHex(0xffffff);
    [self initViewAction];
    
    [self.view addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(155);
        make.height.mas_equalTo(45);
        make.bottom.mas_equalTo(-20);
    }];
    
    UIView *hudongControl = [[UIView alloc] init];
    hudongControl.backgroundColor = UIColor.whiteColor;
    hudongControl.frame = CGRectMake(0,0,34,34);
    
    self.icon = [[UIImageView alloc] init];
    self.icon.backgroundColor = UIColor.whiteColor;
    self.icon.layer.cornerRadius = 17;
    self.icon.clipsToBounds = YES;
    self.icon.contentMode = UIViewContentModeScaleAspectFill;
    [hudongControl addSubview:self.icon];
    self.icon.frame = CGRectMake(0, 0, 34, 34);
    
    UILabel *hudongLab = [[UILabel alloc] init];
    hudongLab.text = @"互动";
    hudongLab.textColor = UIColor.whiteColor;
    hudongLab.font = [UIFont systemFontOfSize:8];
    hudongLab.textAlignment = NSTextAlignmentCenter;
    [hudongControl addSubview:hudongLab];
    hudongLab.frame = CGRectMake(3, 23, 28, 11);
    hudongLab.layer.backgroundColor = UIColorHex(0x444456).CGColor;
    hudongLab.layer.cornerRadius = 5.5f;
    
    [hudongControl addSubview:self.actionBtn];
    self.actionBtn.frame = CGRectMake(-5, -5, 44, 44);

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:hudongControl];

    self.navigationItem.rightBarButtonItems =@[rightItem];
    
    [RACObserve([TT_MessageManager manager], InteractionCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].InteractionCount > 0) {
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.actionBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].InteractionCount];
                self.actionBtn.badgeBGColor = [UIColor redColor];
            });

        } else {
            self.actionBtn.badgeValue = @"";
            self.actionBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
    
    [RACObserve(self.tanhunVC, offset_Y) subscribeNext:^(id x) {

        if (self.tanhunVC.offset_Y <0) {
            return;
        }
        
        if (self.latest_y >= self.tanhunVC.offset_Y) {
            [self showBottom];
        } else {
            [self hideBottom];
        }
        self.latest_y = self.tanhunVC.offset_Y;
        
        if (self.latest_y == 0) {
            [self showBottom];
        }
    }];
    
    [RACObserve(self.lunjiaVC, offset_Y) subscribeNext:^(id x) {

        if (self.lunjiaVC.offset_Y <0) {
            return;
        }
        
        if (self.latest_y >= self.lunjiaVC.offset_Y) {
            [self showBottom];
        } else {
            [self hideBottom];
        }
        self.latest_y = self.lunjiaVC.offset_Y;

        if (self.latest_y == 0) {
            [self showBottom];
        }
    }];
}

- (void)showBottom {
    
    [self.bottomView.superview layoutIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
        
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-20);
        }];

        [self.bottomView.superview layoutIfNeeded];//强制绘制
    }];
    
}

- (void)hideBottom {
    
    [self.bottomView.superview layoutIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
        
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(100);
        }];

        [self.bottomView.superview layoutIfNeeded];//强制绘制
    }];
    
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

}

- (void)initViewAction {
    
    WEAKSELF
    self.huDongView.HuDongFunctionViewBlock = ^(HuDongFunctionViewActionType type) {
      
        [weakSelf.popView dismiss];

        switch (type) {
            case HuDongFunctionViewActionType_close:{
            } break;
            case HuDongFunctionViewActionType_wodetiezi:{
                MyPostListViewController *vc = [[MyPostListViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case HuDongFunctionViewActionType_wodetiwen:{
                MyQAListViewController *vc = [[MyQAListViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case HuDongFunctionViewActionType_wodehuati:{
                MyTopicListViewController *vc = [[MyTopicListViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];

            } break;
            case HuDongFunctionViewActionType_wodepinglun:{
                
                MyEvaluateViewController *vc = [[MyEvaluateViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case HuDongFunctionViewActionType_hudongxiaoxi:{
                
                MyHDSegViewController *vc = [[MyHDSegViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            default:
                break;
        }
    };
    
    self.bottomView.CommunityBottomViewBlock = ^(CommunityBottomViewActionType type) {
      
        switch (type) {
            case CommunityBottomViewActionType_tiezi:{
                PostPublishViewController *vc = [[PostPublishViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            case CommunityBottomViewActionType_huati:{
                TopicPublishViewController *vc = [[TopicPublishViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            case CommunityBottomViewActionType_tiwen:{
                QAPublishViewController *vc = [[QAPublishViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            } break;
            default:
                break;
        }
    };
}

- (void)huzhuControl {    
    [self initHDView];
}

- (void)initHDView {
    
    self.huDongView.frame = CGRectMake(0, 0, TTScreenWidth ,[HuDongFunctionView calculateHeight]);
    self.popView = [LSTPopView initWithCustomView:self.huDongView
                                                popStyle:LSTPopStyleSmoothFromTop
                                            dismissStyle:LSTDismissStyleSmoothToTop];
    self.popView.hemStyle = LSTHemStyleTop;
    
    WEAKSELF
    self.popView.popDuration = 0.5;
    self.popView.dismissDuration = 0.5;
    self.popView.isClickFeedback = YES;
    self.popView.bgClickBlock = ^{

        [weakSelf.popView dismiss];
    };

    [self.popView pop];
}

- (HuDongFunctionView *)huDongView {
    if (!_huDongView) {
        _huDongView = [[NSBundle mainBundle] loadNibNamed:@"HuDongFunctionView" owner:nil options:nil].firstObject;
    }
    return _huDongView;
}

- (CommunityBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[NSBundle mainBundle] loadNibNamed:@"CommunityBottomView" owner:nil options:nil].firstObject;
    }
    return _bottomView;
}

- (void)initPageViewController {
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = UIColorHex(0xff4242);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:24];
    config.titleNormalFont = [UIFont systemFontOfSize:20];
    config.titleViewAlignment = XLPageTitleViewAlignmentLeft;
    config.showTitleInNavigationBar = true;
    config.titleViewStyle = XLPageTitleViewStyleBasic;
    config.separatorLineHidden = true;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeNone;
    config.shadowLineWidth = 20;
    config.titleSpace = 10;
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];

    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.pageViewController registerNib:[UINib nibWithNibName:@"CommunitySegCell" bundle:nil] forTitleViewCellWithReuseIdentifier:@"CommunitySegCellID"];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
//        make.height.mas_equalTo(NavBar_Height);
    }];
}

- (UIButton *)actionBtn {
    if (!_actionBtn) {
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_actionBtn addTarget:self action:@selector(huzhuControl) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {

    if (index == 0) {
        return self.tanhunVC;
    } else {
        return self.lunjiaVC;
    }
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return [self titles][index];
}

- (XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    CommunitySegCell *cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"CommunitySegCellID" forIndex:index];

    [cell fillWithTitle:[self titles][index]];
    return cell;
}

- (NSInteger)pageViewControllerNumberOfPage {
    return [self titles].count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    [self showBottom];
}

- (TanHunMainViewController *)tanhunVC {
    if (!_tanhunVC) {
        _tanhunVC = [[TanHunMainViewController alloc] init];
    }
    return _tanhunVC;
}

- (LunJiaMainViewController *)lunjiaVC {
    if (!_lunjiaVC) {
        _lunjiaVC = [[LunJiaMainViewController alloc] init];
    }
    return _lunjiaVC;
}

#pragma mark -
#pragma mark 标题数据
- (NSArray *)titles {
    return @[@"Ting说",@"Wed答"];
}
@end
